package cn.slt.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.slt.service.ITestExportExcelService;
import cn.slt.service.MD_libraryDSIDService;
//import sun.print.resources.serviceui;

@Controller  
 
public class TestExportExcelController  
{  
	 
//	@ResponseBody
//	public String act(){
//	System.out.println("export");
//	return "1";
//	}
    @Autowired  
    private ITestExportExcelService service;
    @Autowired  
    private MD_libraryDSIDService servicelib;
     
  
    @ResponseBody
    @RequestMapping("xiazai1")
    public String exportExcel_1(@RequestParam String d_owner,HttpServletResponse response)  
    {  
        response.setContentType("application/binary;charset=ISO8859_1"); 
        String mdcode=null;
        System.out.println("d_owner"+d_owner);
        if(mdcode==null){
            mdcode=servicelib.getmdcode(d_owner.toUpperCase());
            System.out.println("mdcode"+mdcode);}
        try  
        {  
            ServletOutputStream outputStream = response.getOutputStream();  
            String fileName = new String(("导出库级元数据到excel").getBytes(), "ISO8859_1");  
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式  
            String hql = "cn.slt.mapper.MD_libraryDSIDMapper.getAll";  
            String[] titles = { "元数据编码", "数据集编码", "数据集名称（owner用户名）" ,"数据覆盖起始时间","数据库创建单位", "数据库创建者（岗位）","数据库创建日期", "数据库管理单位","数据库管理者（岗位）","数据库建库标准名称","对数据库服务服务器访问说明","数据库URL","数据库字符集","关键词","数据库概述","数据库版本",
            		"数据库数据使用注意事项","使用限制级别","安全限制级别","其他限制","交换分发环境"};  
            service.exportExcel1(hql,mdcode, titles, outputStream);  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        return null;  
    }  
    
    @ResponseBody
    @RequestMapping("xiazai2")
    public String exportExcel_2(@RequestParam String d_owner,HttpServletResponse response)  
    {  
        response.setContentType("application/binary;charset=ISO8859_1"); 
        String mdcode=null;
        System.out.println("d_owner"+d_owner);
        if(mdcode==null){
            mdcode=servicelib.getmdcode(d_owner.toUpperCase());
            System.out.println("mdcode"+mdcode);}
        try  
        {  
            ServletOutputStream outputStream = response.getOutputStream();  
            String fileName = new String(("导出表级元数据到excel").getBytes(), "ISO8859_1");  
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式  
            String hql = "cn.slt.mapper.MD_ClassDSIDMapper.getAll";  
            String[] titles = { "元数据编码", "上级数据集编码", "数据集编码","数据集名称（表名）" ,"创建时间","表备注","表关键词" };  
            service.exportExcel2(hql,mdcode, titles, outputStream);  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        return null;  
    }  
    
    
    @ResponseBody
    @RequestMapping("xiazai3")
    
    public String exportExcel_3(@RequestParam String d_owner,HttpServletResponse response)  
    {  
        response.setContentType("application/vnd.ms-excel;charset=ISO8859_1"); 
        String mdcode=null;
        System.out.println("d_owner"+d_owner);
        if(mdcode==null){
            mdcode=servicelib.getmdcode(d_owner.toUpperCase());
            System.out.println("mdcode"+mdcode);}
        try  
        {   
        	
           
            
            ServletOutputStream outputStream = response.getOutputStream();  
            String fileName = new String(("导出字段级元数据到excel").getBytes(), "ISO8859_1");  
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式  
            String hql = "cn.slt.mapper.MD_ObjDSIDMapper.getAll";  
            String[] titles = { "元数据编码", "上级数据集编码", "数据集编码","数据集名称（字段名）" ,"是否为主键","数据类型","创建时间","字段备注","该字段下记录重大变化记录"};  
            System.out.println("mdcode:ser"+mdcode);
          
           
            service.exportExcel3(hql,mdcode, titles, outputStream); 
            if(mdcode!=null) return null;
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
       
        return null;  
    }  
    
    @ResponseBody
    @RequestMapping("pipeidownload")
    
    public String exportExcel_4(HttpServletRequest request,HttpServletResponse response)  
    {  
    	String location = request.getParameter("location");
    	String owner = request.getParameter("owner");
    	
    	System.out.println(location+" ---- "+owner);
        response.setContentType("application/vnd.ms-excel;charset=utf-8"); 
        try  
        {   
            ServletOutputStream outputStream = response.getOutputStream();  
            String fileName = new String(("数据库填写模板").getBytes(), "ISO8859_1");  
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式  
            String[] titles = { "supdscode","dscode","表/字段名", "中文解释", "备注"};  
            service.exportExcel4(location,owner,titles, outputStream); 
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
       
        return null;  
    }  
    
    @ResponseBody
    @RequestMapping("exceldownload")
    
    public String exportExcel_5(HttpServletRequest request,HttpServletResponse response)  
    {  
    	String location = "jdbc:oracle:thin:@121.248.200.5:1521:orcl";
    	String owner = "SWJSYSQ";
    	
    	System.out.println(location+" ---- "+owner);
        response.setContentType("application/vnd.ms-excel;charset=utf-8"); 
        try  
        {   
            ServletOutputStream outputStream = response.getOutputStream();  
            String fileName = new String(("数据库填写模板").getBytes(), "ISO8859_1");  
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式  
            
            service.exportExcel5( outputStream); 
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
       
        return null;  
    }  
}